package com.servlet.yanglaoyuan.controller;

import com.servlet.yanglaoyuan.common.Result;
import com.servlet.yanglaoyuan.pojo.Medication_Record;
import com.servlet.yanglaoyuan.pojo.dataFilter;
import com.servlet.yanglaoyuan.service.Medication_RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
public class MedicationController {
@Autowired
private Medication_RecordService medication_recordService;
    @PostMapping("/MedicationData")
    public Result add(@RequestBody Medication_Record record) {
        log.info("接收到的添加用药数据: {}", record);
        if (record.getElderlyId() == null) {
            log.warn("elderlyId 不能为空");
            return Result.error("elderlyId 不能为空");
        }
        medication_recordService.add(record);
        return Result.success("添加成功");
    }

    @GetMapping("/MedicationData/{id}")
    public Result findAllByElderlyId(@PathVariable Integer id) {
        log.info("获取Id为："+id +"的所有检查数据");
        return Result.success(medication_recordService.getByElderlyId(id));
    }

    @DeleteMapping("/MedicationData/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除Id为："+id +"的用药数据");
        medication_recordService.deleteRecord(id);
        return Result.success();
    }

    @PutMapping("/MedicationData/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Medication_Record record) {
        log.info("更新Id为"+id+"的用药数据");
        log.info("更新数据: {}", record);
        LocalDateTime date = record.getDate();
        if (date != null) {
            LocalDateTime tomorrow = date.plusDays(1);
            log.info("接收到的时间: {}", date);
            log.info("明天的时间: {}", tomorrow);
            date = tomorrow;
            // 可以在此进行时间比较或其他业务逻辑处理
        } else {
            log.warn("时间字段为空");
        }
        record.setId(id);
        record.setDate(date);
        medication_recordService.updateRecord(record);
        return Result.success();
    }

    @PostMapping("/MedicationDataFilter")
    public Result getRecord(@RequestBody dataFilter record) {
        log.info("筛选出的用药数据: {}", record);
        if (record.getElderlyId() == null) {
            log.warn("elderlyId 不能为空");
            return Result.error("elderlyId 不能为空");
        }
        LocalDateTime dateStart = record.getDateStart();
        LocalDateTime dateEnd = record.getDateEnd();
        if (dateStart != null && dateEnd != null) {
            LocalDateTime tomorrowStart = dateStart.plusDays(1);
            LocalDateTime tomorrowEnd = dateEnd.plusDays(1);
            dateStart = tomorrowStart;
            dateEnd = tomorrowEnd;
            // 可以在此进行时间比较或其他业务逻辑处理
        } else {
            log.warn("时间字段为空");
        }
        record.setDateStart(dateStart);
        record.setDateEnd(dateEnd);
        return Result.success(medication_recordService.getRecord(record));
    }
}
